package com.asset.pdf;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PdfGenerator {
    public static File main(String[] args) {
        String outputFile = "C:\\Users\\Administrator\\Desktop\\test\\output.pdf"; // 输出文件名

        try {
            // 创建 PdfWriter 对象
            PdfWriter writer = new PdfWriter(outputFile);


            // 创建 PdfDocument 对象
            PdfDocument pdf = new PdfDocument(writer);

            // 创建 Document 对象
            Document document = new Document(pdf, new PageSize(283.5f, 198.5f));
            document.setMargins(0, 0, 0, 0);

            // 创建表格
            Table table = new Table(new float[]{80f, 203.f}); // 3 列的表格
            table.setWidth(283f);// 设置表格宽度占页面宽度的百分比
            table.setHeight(198f);
            table.setBorder(new SolidBorder(1f));


            // 使用SimSun字体（宋体）来显示中文
//            PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", true);
            PdfFont font =
                    PdfFontFactory.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", PdfEncodings.IDENTITY_H,true);

            // 合并第一行的两个单元格
            Cell mergedCell = new Cell(1, 2); // 跨1行、2列
            mergedCell.setBorder(new SolidBorder(1f));
            Paragraph paragraph = createParagraph(font, "固定资产标识卡");
            paragraph.setCharacterSpacing(20f);
            mergedCell.add(paragraph);
            table.addCell(mergedCell);

            List<String> list = new ArrayList<>();
            list.add("资产名称");
            list.add("资产编号");
            list.add("规格型号");
            list.add("使用部门");
            list.add("使用时间");
            list.add("使用人");

            HashMap<String, String> map = new HashMap<>();
            map.put("资产名称", "笔记本电脑");
            map.put("资产编号", "015219");
            map.put("规格型号", "p14");
            map.put("使用部门", "信息化办公室");
            map.put("使用时间", "2000-10");
            map.put("使用人", "江响进");


            for (String s : list) {
                insertCellToTable(table, s, font, map.get(s));
            }


            document.add(table);
            // 关闭 Document 对象
            document.close();


            System.out.println("成功生成 PDF 文件：" + outputFile);
        } catch (FileNotFoundException e) {
            System.err.println("文件未找到异常：" + e.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new File(outputFile);
    }

    private static void insertCellToTable(Table table, String text, PdfFont font, String key) throws IOException {
        Cell cell1 = new Cell();
        cell1.setBorder(new SolidBorder(1f));
        Paragraph paragraph = createParagraph(font, text);
        cell1.add(paragraph);
        table.addCell(cell1);

        Cell cell2 = new Cell();
        cell2.setBorder(new SolidBorder(1f));
        Paragraph paragraph2 = createParagraph(font, key);
        cell2.add(paragraph2);
        table.addCell(cell2);
    }


    public static Paragraph createParagraph(PdfFont font, String text) {
        Paragraph paragraph = new Paragraph(text);
        paragraph.setFont(font);
        paragraph.setTextAlignment(TextAlignment.CENTER);
        return paragraph;
    }
}
